<?php  if ( ! defined('EXT')) exit('Invalid file request');

// Getting the Weblog class.
include_once(dirname(__FILE__).'/../weblog/mod.weblog.php');

/**
 * Weblog Extended
 *
 * An ExpressionEngine Module that allows you to work with the
 * new resources added by the Weblog Extended extension.
 *
 * @package		ExpressionEngine
 * @author		Mozart Petter
 * @copyright	Copyright (c) 2008, Mozart Petter.
 * @license		http://creativecommons.org/licenses/by-sa/2.5/br/deed.en_US
 * @link		http://projects.mozartpetter.com/projects/show/eewextended
 * @filesource
 * 
 * This work is licensed under the Creative Commons Attribution-Share Alike 2.5 Brazil.
 * To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/2.5/br/deed.en_US
 */
class Weblog_Extended extends Weblog
{
	
	/**
	 * Constructor.
	 * 
	 * @return 
	 */
	public function Weblog_Extended()
	{
		parent::Weblog();
	}
	
	/**
	 * List one or more weblogs.
	 * 
	 * @return String
	 */
	public function weblogs()
	{
		global $DB, $TMPL, $PREFS;

		// Parameters.
		$params = array('weblog_id', 'parent_only', 'site_id', 'status', 'orderby', 'sort', 'style', 'offset', 'limit', 'id', 'class');
		
		// Fetching params.
		foreach ($params as $param)
		{
			$$param = $TMPL->fetch_param($param);
		}
		
		$parent_id = ($parent_only != 'yes') ? '' : 0;
		
		// How we'll show the data?
		$style = ($style !== FALSE) ? $style : 'linear';
		
		// Tags to be replaced.
		$tags = array('{id}', '{name}', '{title}', '{url}', '{description}', '{total_entries}', '{total_comments}', '{total_trackbacks}', '{last_entry_date}', '{status}', '{parent_id}');
		
		// The HTML code that will be returned.
		$html = "";
		
		// Holds the created items.
		$items = array();
		
		// If the style type is 'nested'.
		if ($style == "nested")
		{
			$html .= "<ul";
			if ($id) $html .=  " id='$id'";
			if ($class) $html .= " class='$class'";
			$html .= ">";
		}
		
		// Foreach result.
		foreach ($this->getWeblogChildren($parent_id, $weblog_id, $site_id, $status, $orderby, $sort, $limit, $offset) as $item)
		{
			// If the item don't already exists on the list.
			if (array_search($item, $items, true) === FALSE)
			{
				// Adding the new item to the list.
				array_push($items, $item);
				
				// Values that will replace the tags.
				$values = array($item['weblog_id'], $item['blog_name'], $item['blog_title'], $item['blog_url'], $item['blog_description'], $item['total_entries'], $item['total_comments'], $item['total_trackbacks'], $item['last_entry_date'], $item['blog_status'], $item['blog_parent']);
				
				// If the style type is 'nested'.
				if ($style == "nested") 
				{
					$html .= "<li>";
					$html .= str_replace($tags, $values, $TMPL->tagdata);
				}
				else
				{
					$html .= str_replace($tags, $values, $TMPL->tagdata);
				}
				
				// Getting the children of the current weblog, if they exists.
				$children = $this->getWeblogChildren($item['weblog_id'], '', $site_id, $status, $orderby, $sort);
				
				// If we can show the children, and if there are any.
				if ($parent_only != "yes" && $children)
				{
					// If the style type is 'nested'.
					if ($style == "nested") $html .= "<ul>";
				
					// For each child.
					foreach ($children as $child)
					{
						// Adding the child item to the list.
						array_push($items, $child);
						
						// Values that will replace the tags.
						$cvalues = array($child['weblog_id'], $child['blog_name'], $child['blog_title'], $child['blog_url'], $child['blog_description'], $child['total_entries'], $child['total_comments'], $child['total_trackbacks'], $child['last_entry_date'], $child['blog_status'], $child['blog_parent']);
						
						// If the style type is 'nested'.
						if ($style == "nested")
						{
							$html .= "<li>";
							$html .= str_replace($tags, $cvalues, $TMPL->tagdata);
							$html .= "</li>";
						}
						else
						{
							$html .= str_replace($tags, $cvalues, $TMPL->tagdata);
						}
					}
					
					// If the style type is 'nested'.
					if ($style == "nested") $html .= "</ul>";
				}
				
				$html .= "</li>";
			}
		}
		
		// If the style type is 'nested'.
		if ($style == "nested") $html .= "</ul>";
		
		return $html;
	}
	
	/**
	 * Returns the children of the specified parent.
	 * 
	 * @return Array
	 * @param $parent Number Id of the weblog
	 */
	private function getWeblogChildren($parent = FALSE, $weblog_id = '', $site_id = '', $status = '', $orderby = '', $sort = '', $limit = '', $offset = '')
	{
		global $DB, $PREFS;
		
		// Creating SQL.
		$sql = 	"SELECT 
					weblog_id, blog_name, blog_title, blog_url, blog_description, total_entries, total_comments, total_trackbacks, last_entry_date, blog_status, blog_parent
				FROM
					exp_weblogs
				";
				
		// If a site id don't exists.
		if ($site_id !== '' && $site_id === FALSE) $site_id = $DB->escape_str($PREFS->ini('site_id'));
		
		$sql .= "WHERE
					site_id = $site_id ";
		
		// If parent is setted.		
		if ($parent !== '' && ($weblog_id === FALSE || $weblog_id === ''))
		{
			// Getting only the parents.
			$sql .= "AND
						blog_parent = $parent 
					";
		}
		
		// If weblog_id has a value.
		if ($weblog_id !== '' && $weblog_id !== FALSE)
		{
			$sql .= "AND
						weblog_id = $weblog_id ";
		}
		
		// If status has a value.
		if ($status !== '' && $status !== FALSE)
		{
			$sql .= "AND
						blog_status = '$status' ";
		}
		
		$sql .= "ORDER BY ";
		
		// If orderby has a value.
		if ($orderby !== '' && $orderby !== FALSE && stristr($orderby, '|'))
		{
			$order_array = explode("|", $orderby);
		}
		else if ($orderby !== '' && $orderby !== FALSE)
		{
			$order_array = array($orderby);
		}
		else
		{
			$order_array = array('blog_parent', 'blog_order');
		}
		
		// If sort has a value.
		if ($sort !== '' && $sort !== FALSE && stristr($sort, '|'))
		{
			$sort_array = explode("|", $sort);
		}
		else if ($sort !== '' && $sort !== FALSE)
		{
			$sort_array = array($sort);
		}
		else
		{
			$sort_array = array('ASC');
		}
		
		// Creating order for the SQL.
		foreach ($order_array as $key => $order)
		{
			$sql .= ($key == 0) ? $order : ", " . $order;
			
			if (isset($sort_array[$key]))
			{
				$sql .= " " . $sort_array[$key];
			}
			else
			{
				$sql .= " " . $sort_array[0];
			}
		}
		
		// Limit and offset.
		if ($limit !== '' && $limit !== FALSE && $limit != 0)
		{
			$sql .= " LIMIT";
			
			if ($offset !== '' && $offset !== FALSE)
			{
				$sql .= " $offset, $limit";
			}
			else
			{
				$sql .= " $limit";
			}
		}
		
		// Executing SQL.
		$query = $DB->query($sql);
		
		return $query->result;
	}
	
}

?>
